import { Component, OnInit } from '@angular/core';
import { NavController, Platform } from '@ionic/angular';
import { HttpService } from 'src/app/service/http.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { UrlService } from 'src/app/service/url.service';
import { ToolService } from 'src/app/service/tool.service';
import { ConfigService } from 'src/app/service/config.service';
import { Clipboard } from '@ionic-native/clipboard/ngx';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
@Component({
  selector: 'app-join',
  templateUrl: './join.page.html',
  styleUrls: ['./join.page.scss'],
})
export class JoinPage implements OnInit {
  public joinData = { "agreement": "", "mail": "", "open": "", "qq": "", "skype": "", "stranger": "", "wechant": "","qq_no":"" };
  public stateData = { "isAgreement": false, "isStranger": false, "isLink": true };
  public showWin=false;
  constructor(private navCtrl: NavController, private http: HttpService, private popWin: PopwinService, private clipboard: Clipboard, private platform: Platform,private iab:InAppBrowser) { }

  ngOnInit() {
    this.joinNet();
  }
  backClick() {
    this.navCtrl.navigateBack("tabs/my");
  }

  changeTab(typeStr: String) {
    switch (typeStr) {
      case 'stranger': {
        this.stateData = { "isAgreement": false, "isStranger": true, "isLink": false };
        break;
      }
      case 'agreement': {
        this.stateData = { "isAgreement": true, "isStranger": false, "isLink": false };
        break;
      }
      case 'link': {
        this.stateData = { "isAgreement": false, "isStranger": false, "isLink": true };
        break;
      }
    }
  }

  joinNet() {
    let that = this;
    let obj = "{\"type\":\"" + "join" + "\"}";
    this.http.get(UrlService.OTHER_CONFIG, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let res = ToolService.aesDecodeToJson(temp);
        that.joinData = res;
        console.log(res);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  copyFun(text: any) {
    if (this.platform.is("cordova")) {
      this.clipboard.copy(text).then(() => {
        this.popWin.toast("已复制到剪切板");
      });
    } else {
      let save = function (e) {
        e.clipboardData.setData('text/plain', text);   //保存到剪切板
        e.preventDefault(); //阻止默认行为
      }
      document.addEventListener('copy', save);
      document.execCommand("copy");     //使文档处于可编辑状态，否则无效
      this.popWin.toast("已复制到剪切板");
    }
  }

  copyText(text: any) {
    this.copyFun(text);
  }
  /**
   * 打开QQ客服
   */
  toQQ(val) {
    if(ToolService.isEmpty(val)){
      this.popWin.toast("暂无客服联系方式");
        return;
    }else{
      const browser = this.iab.create(val,'_system');
    }
  }
  /**
   * 跳转到微信
   */
  toEwm(){
    this.showWin=true;
  }

  hideWin(){
    this.showWin=false;
  }
}
